package com.redstar.HappyRefresh.service.impl;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import com.redstar.HappyRefresh.config.QiNiuConfig;
import com.redstar.HappyRefresh.service.QiNiuFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.File;


@Service
@Slf4j
public class QiNiuFileServiceImpl implements QiNiuFileService { // 七牛云文件服务

    @Autowired
    private QiNiuConfig qiNiuConfig; // 七牛云配置

    @Override
    public String getToken() { // 生成专门用于视频的上传Token，附带了漏斗操作
        return qiNiuConfig.videoUploadToken();
    }

    @Override
    public String uploadFile(File file) { // 上传文件
        // 构造一个带指定Region对象的配置类，Region.region0()为指定的区域，与存储空间的地理位置一致
        Configuration cfg = new Configuration(Region.region0());
        // 七牛云SDK提供的用于文件上传的管理器
        UploadManager uploadManager = new UploadManager(cfg);
        try {
            // 上传文件
            Response response = uploadManager.put(file,null,qiNiuConfig.videoUploadToken());
            // 通过Gson将响应体反序列化为DefaultPutRet对象，该对象包含了上传成功的文件信息
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            // 获取文件的键，key
           return putRet.key;
        } catch (QiniuException ex) {
            log.error(ex.getMessage(), ex);
            if (ex.response != null) {
                log.error(ex.response.toString());
                try {
                    log.error(ex.response.toString());
                } catch (Exception ignored) {
                    log.error(ex.getMessage(), ex);
                }
            }
        }
        return null;
    }

    @Override
    @Async
    public void deleteFile(String url) { // 删除文件
        // 构造一个带指定Region对象的配置类，Region.region0()为指定的区域，与存储空间的地理位置一致
        Configuration cfg = new Configuration(Region.region0());
        String bucket = qiNiuConfig.getBucketName();
        Auth auth = qiNiuConfig.buildAuth();
        String key = url;
        // 用于进行存储桶相关的操作，包括文件的删除
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, key);
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            log.error(String.valueOf(ex.code()));
            log.error(ex.response.toString());
        }
    }

    @Override
    public FileInfo getFileInfo(String url) { // 根据URL获取文件信息
        // 构造一个带指定Region对象的配置类，Region.region0()为指定的区域，与存储空间的地理位置一致
        Configuration cfg = new Configuration(Region.region0());
        Auth auth = qiNiuConfig.buildAuth();
        String bucket = qiNiuConfig.getBucketName();
        // 用于进行存储桶相关的操作，包括文件的查询
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            return bucketManager.stat(bucket, url);
        } catch (QiniuException ex) {
            log.error(ex.response.toString());
        }
        return null;
    }
}
